﻿<cfscript>

/** 
* System Security MessageDigest Frameworks
* 
* @displayname "public.service.util.digestAdvice" 
* @hint "System Security MessageDigest Frameworks" 
* 
* @Output false
* 
* @accessors true
*/ 

component {
	
	property type="string" name="Algorithm" hint="加密算法";
	property type="string" name="Key" hint="加密字符串";
	
	/**
	* @hint "初始化对象"
	* 
	* @Output false
	* 
	*/
	public function init( string Algorithm="SSHA", string encriptKey="Nope!" ) {
		
		setAlgorithm( arguments.Algorithm );
		setKey( arguments.encriptKey );
		
		return this;
	}
	
	
	/**
	* @hint "加密字符串"
	* 
	* @Output false
	* 
	* @originalString "明文"
	* @key "密钥"
	*/
	public string function encryptString( required string originalString, string salt="", string key=getKey() ) {
		
		var digestString = "";
		
		try {
			digestString = lCase( encrypt( arguments.originalString, arguments.key & arguments.salt, "PBEWithMD5AndDES", "hex" ) );
		}
		catch ( any exception ) {
		}
		return digestString;
	}
	
	
	/**
	* @hint "解密字符串"
	* 
	* @Output false
	* 
	* @digestString "密文"
	* @key "密钥"
	*/
	public string function decryptString( required string digestString, string salt="", string key=getKey() ) {
		
		var originalString = "";
		
		try {
			originalString = decrypt( arguments.digestString, arguments.key & arguments.salt, "PBEWithMD5AndDES", "hex" );
		}
		catch ( any exception ) {
		}
		
		return originalString;
	}
	
	
	/**
	* @hint "生成加密密码密文"
	* 
	* @Output false
	* 
	* @originalString "密码明文"
	* @algorithm "加密算法"
	*/
	public string function createDigest( required string originalString, string algorithm=getAlgorithm() ) {
		
		switch( arguments.algorithm ) {
			
			case "SSHA":
				return createSSHAPassword( arguments.originalString );
				break; 
			
			case "FAYEA":
				return createPassword( arguments.originalString );
				break;
		}
		
		return "";
	}
	
	
	/**
	* @hint "比较密码明文与密文是否匹配"
	* 
	* @Output false
	* 
	* @digestString "密码密文"
	* @originalString "密码明文"
	*/
	public boolean function checkDigest( required string digestString, required string originalString ) {
		
		if ( left( arguments.digestString, 6 ) eq "{SSHA}" ) {
			return compareSSHAPassword( arguments.digestString, arguments.originalString );
		}
		
		if ( left( arguments.digestString, 7 ) eq "{FAYEA}" ) {
			return comparePassword( right( arguments.digestString, len( arguments.digestString )-7 ), arguments.originalString );
		}
		
		return false;
	}
	
	
	/**
	* @hint "比较 SSHA 密码"
	* 
	* @Output false
	* 
	* @digestString "密码密文"
	* @originalString "密码明文"
	*/
	public boolean function compareSSHAPassword( required string digestString, required string originalString ) {
		
		var oper = createObject("java", "com.fayea.security.ldap.SSHA").init("SHA-256");
		
		return oper.checkDigest( javaCast("String", arguments.digestString), javaCast("String", arguments.originalString) );
	}
	
	
	/**
	* @hint "生成 SSHA 加密方式密码"
	* 
	* @Output false
	* 
	* @originalString "密码明文"
	*/
	public string function createSSHAPassword( required string originalString ) {
		
		var oper = createObject("java", "com.fayea.security.ldap.SSHA").init("SHA-256");
		
		return oper.createDigest( javaCast("String", arguments.originalString) );		
	}
	

	/**
	* @hint "比较默认加密密码"
	* 
	* @Output false
	* 
	* @digestString "密码密文"
	* @originalString "密码明文"
	* @key "密钥"
	*/
	public boolean function comparePassword( required string digestString, required string originalString, string key=getKey() ) {
		
		var decryptString = "";
		
		try {
			decryptString = decrypt( arguments.digestString, arguments.key, "PBEWithMD5AndTripleDES", "base64" );
			
			if ( compare( decryptString, arguments.originalString ) eq 0 ) {
				return true;
			}
		}
		catch ( any exception ) {
		}
		
		return false;
	}
	
	
	/**
	* @hint "生成默认加密密码"
	* 
	* @Output false
	* 
	* @originalString "密码明文"
	* @key "密钥"
	*/
	public string function createPassword( required string originalString, string key=getKey() ) {
		
		var hashString = "";
		
		try {
			hashString = "{FAYEA}" & encrypt( arguments.originalString, arguments.key, "PBEWithMD5AndTripleDES", "base64" );
		}
		catch ( any exception ) {
		}
		
		return hashString;
	}
	
}

</cfscript>